package com.example.yls.demoa;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnstar;
    private Button btnstop;
    private Button btnBind;
    private Button btnunBind;
    private ServiceConnection serviceConnection;
    private MyBinder binder;
    private Button btnadd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findView();
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.e("MainActivity.this", "onServiceConnected");
                binder = (MyBinder) iBinder;
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.e("MainActivity.this", "onServiceDisconnected");
            }
        };
        /*绑定服务*/
        btnBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                bindService(intent, serviceConnection, BIND_AUTO_CREATE);
            }
        });
        /*添加*/
        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int result = binder.add(100, 200);
                Log.e("binder.add", "result=" + result);
                String str = binder.toUpperCase("hello");
                Log.e("binder.add", "str=" + str);

            }
        });
        /*取消绑定*/
        btnunBind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(serviceConnection);
            }
        });

        /*启动服务*/
        btnstar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                startService(intent);
            }
        });
        /*停止服务*/
        btnstop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, MyService.class);
                stopService(intent);
            }
        });
    }
/*绑定事件的方法*/
    private void findView() {
        btnstar = findViewById(R.id.star_service);
        btnstop = findViewById(R.id.stop_service);
        btnBind = findViewById(R.id.bind_service);
        btnunBind = findViewById(R.id.unbind_service);
        btnadd = findViewById(R.id.btn_add);
    }

}
